<?php
/**
 * Created by PhpStorm.
 * User: apple
 * Date: 2018/11/29
 * Time: 1:16 PM
 */
namespace app\shop\model;
use think\Db;
use app\shop\model\Promotion as PromotionModel;

/**
 * Class Goods
 * @package app\shop\model
 * @OA\Schema(schema="Goods", description="商品模型", type="object",
 *   @OA\Property(property="goods_id", description="商品ID", type="integer", ),
 *   @OA\Property(property="goods_name", description="商品名称", type="string", ),
 *   @OA\Property(property="brand_id", description="品牌ID", type="integer", ),
 *   @OA\Property(property="brand", description="品牌信息", ref="#/components/schemas/Brand", ),
 *   @OA\Property(property="cate_id", description="分类ID", type="integer", ),
 *   @OA\Property(property="category", description="分类信息", ref="#/components/schemas/Category", ),
 *   @OA\Property(property="goods_images", description="商品图片", type="array", @OA\Items( type="string", ), ),
 *   @OA\Property(property="defaultImage", description="商品默认图片", type="string", ),
 *   @OA\Property(property="price", description="商品价格", type="number", ),
 *   @OA\Property(property="stock", description="商品库存", type="integer", ),
 *   @OA\Property(property="goods_desc", description="商品详情", type="string", ),
 *   @OA\Property(property="is_new", description="是否新品", type="boolean", ),
 *   @OA\Property(property="is_hot", description="是否热卖商品", type="boolean", ),
 *   @OA\Property(property="is_recommend", description="是否推荐商品", type="boolean", ),
 *   @OA\Property(property="is_promote", description="是否促销商品", type="boolean", ),
 *   @OA\Property(property="is_discount", description="是否打折商品", type="boolean", ),
 *   @OA\Property(property="skus", description="商品单品", type="array", @OA\Items(ref="#/components/schemas/GoodsSku"), ),
 *   @OA\Property(property="sku_default", description="默认单品ID", type="integer", format="int32", ),
 *   @OA\Property(property="defaultSku", description="默认单品", ref="#/components/schemas/GoodsSku", ),
 *   @OA\Property(property="specs", description="商品所有规格", type="array", @OA\Items(ref="#/components/schemas/GoodsSpec"), ),
 * )
 */
class Goods extends \ShopModel
{
    protected $table = 'sh_goods';

//    protected $type = [
//        'price' => 'float',
//    ];

    use \app\shop\traits\Goods;

    /**
     * 单品
     */
//    public function sku()
//    {
//        return $this->belongsTo('GoodsSku', 'sku_id', 'sku_id');
//    }
//    public function levelPrice(){
//        return $this->belongsTo('GoodsLevelPrice', 'level_price_id');
//    }
//sh_goods_level_price
    public function levelprice()
    {
        return $this->belongsTo('GoodsLevelPrice', 'goods_id', 'goods_id');
    }
    public function clientprice(){
        return $this->belongsTo('GoodsClientPrice', 'goods_id', 'goods_id');
    }

    public function GoodsDb(){
        return Db::name('goods');
    }


    /**
     * 搜索
     */
    public function search($params){
        $page = !empty($params['page']) ? $params['page'] : 1;
        $pageSize = !empty($params['pageSize']) ? $params['pageSize'] : 20;
        $where = [];

        //单个商品
        if(isset($params['goods_id'])&&!empty($params['goods_id'])){
            $where['goods.goods_id'] = $params['goods_id'];
        }
        //是否有登陆
        if(isset($params['client_id'])&&!empty($params['client_id'])){

        }
        //商品分类id

        if(!empty($params['cate_id'])){
            $where['goods.cate_id'] = $params['cate_id'];
        }
        //商品是否上架
        if(!empty($params['is_putaway'])){
            $where['goods.is_putaway'] = $params['is_putaway'];
        }
        //搜索
        if(!empty($params['query'])){
            $where['goods.goods_name | goods.goods_sn | goods.specification1_value | goods.specification2_value  | goods.specification3_value  | goods.keywords | client.goods_code'] = ['like','%'.$params['query'].'%'];
        }
        //商品标签
        if(!empty($params['is_new'])){
            $where['goods.is_new'] = $params['is_new'];
        }
        if(!empty($params['is_hot'])){
            $where['goods.is_hot'] = $params['is_hot'];
        }

        //所属仓库
        if(!empty($params['warehouse_id'])){
            $where['goods.warehouse_id'] = $params['warehouse_id'];
        }
        if(!empty($params['is_img'])){
            $where['goods.goods_img'] = null;
        }
        //
        $data = $this->alias('goods')
            ->join('sh_goods_units units','goods.goods_id = units.goods_id','left')
            ->join('sh_goods_unit unit','units.initial_unit_id = unit.unit_id','left')
            //->join('sh_goods_level_price pri','goods.goods_id =pri.goods_id')
            ->where($where)
            ->field('goods.goods_id,goods.specification_id,goods.goods_img,goods.goods_sn,goods.goods_name,goods.specification1_name,goods.specification2_name,goods.specification3_name,goods.specification1_value,goods.specification2_value,goods.specification3_value,unit.unit_name,units.initial_unit_price,goods.cost_price,goods.goods_inventory,goods.add_goods_time,goods.update_goods_time')
            ->where(  'goods.is_delete' ,0)
            ->page($page,$pageSize)
            ->order('goods.add_goods_time desc')
            ->select();
        $GoodsLevelPrice = new \app\shop\model\GoodsLevelPrice();
        $ClientLevelModel =  new \app\shop\model\ClientLevel();
       $GoodsLevelPriceDb =  $GoodsLevelPrice->GoodsLevelPriceDb();
        $discount = $ClientLevelModel->column('discount','level_id');

        foreach ($data as $k=>$v){
            $data[$k]['specification'] = $data[$k]['specification1_name'].':'.$data[$k]['specification2_value'].','.$data[$k]['specification1_name'].':'.$data[$k]['specification2_value'].','.$data[$k]['specification3_name'].':'.$data[$k]['specification3_value'];
            unset($data[$k]['specification1_name']);
            unset($data[$k]['specification2_name']);
            unset($data[$k]['specification3_name']);
            unset($data[$k]['specification1_value']);
            unset($data[$k]['specification2_value']);
            unset($data[$k]['specification3_value']);
            $data[$k]['level_price'] =$GoodsLevelPriceDb->where('goods_id',$v['goods_id'])->column('level_price1','level_id');
            $data[$k]['level_price_units'] =$GoodsLevelPriceDb->where('goods_id',$v['goods_id'])->column('level_price1,level_price2,level_price1','level_id');
            $data[$k]['add_goods_time'] = date('Y-m-d H:i:s', $data[$k]['add_goods_time']);
            if(!empty($data[$k]['update_goods_time']))$data[$k]['update_goods_time'] = date('Y-m-d H:i:s', $data[$k]['update_goods_time']);
        }
        return $data;
    }
    /**
     * 搜索(区别用户)
     */
    public function search2($params){
        $page = !empty($params['page']) ? $params['page'] : 1;
        $pageSize = !empty($params['pageSize']) ? $params['pageSize'] : 20;
        $where = [];
        //单个商品
        if(isset($params['goods_id'])&&!empty($params['goods_id'])){
            $where['goods.goods_id'] = $params['goods_id'];
        }
        //商品分类id
        if(!empty($params['cate_id'])){
            $where['goods.cate_id'] = $params['cate_id'];
        }
        //商品是否上架
        if(!empty($params['is_putaway'])){
            $where['goods.is_putaway'] = $params['is_putaway'];
        }
        //搜索
        if(!empty($params['query'])){
            $where['goods.goods_name | goods.goods_sn | goods.specification1_value | goods.specification2_value  | goods.specification3_value  | goods.keywords | goods.goods_code'] = ['like','%'.$params['query'].'%'];
        }
        //商品标签
        if(!empty($params['is_new'])){
            $where['goods.is_new'] = $params['is_new'];
        }
        if(!empty($params['is_hot'])){
            $where['goods.is_hot'] = $params['is_hot'];
        }

        //所属仓库
        if(!empty($params['warehouse_id'])){
            $where['goods.warehouse_id'] = $params['warehouse_id'];
        }
        if(!empty($params['is_img'])){
            $where['goods.goods_img'] = null;
        }
        //

        $data = $this->GoodsDb()->alias('goods')
            ->where($where)
            ->field('goods.goods_id,goods.specification_id,goods.goods_img,goods.goods_sn,goods.goods_name,goods.specification1_name,goods.specification2_name,goods.specification3_name,goods.specification1_value,goods.specification2_value,goods.specification3_value,goods.goods_inventory')
            ->where(  'goods.is_delete' ,0)
            ->where('goods_state',1)
            ->page($page,$pageSize)
            ->order('goods.add_goods_time desc')
            ->select();
        $GoodsUnitModel = new GoodsUnits();
        $GoodsUnitsDb = $GoodsUnitModel->GoodsUnitsDb();
        $GoodsUnitsModel = new Unit();
        $Units =  $GoodsUnitsModel->where('is_delete',0)->column('unit_name','unit_id');
        $ClientModel = new Client();
        $level_id = $ClientModel->where('client_id',$params['client_id'])->value('level_id');
        $GoodsLevelPriceModel =  new GoodsLevelPrice();
        $GoodsLevelPriceDb = $GoodsLevelPriceModel->GoodsLevelPriceDb();

        foreach ($data as $k=>$v){
            //查询是否有指定客户可购
            $ClientGoodsPriceData = $this->getClientGoodsPrice($params['client_id'],$v['goods_id']);
            $Unit = $GoodsUnitsDb->where('goods_id',$v['goods_id'])->find();
            //判断单位是否可购
            $_d[0] = $Unit['is_use_initial_unit'];
            $_d[1]= $Unit['is_use_extra1_unit'];
            $_d[2] = $Unit['is_use_extra2_unit'];
            $data[$k]['unit']['unit1']['id'] =$Unit['initial_unit_id'];
            $data[$k]['unit']['unit1']['name'] =$Units[$Unit['initial_unit_id']];
            if($_d[1])$data[$k]['unit']['unit2']['id'] = $Unit['extra1_unit_id'];
            if($_d[1])$data[$k]['unit']['unit2']['name'] = $Units[$Unit['extra1_unit_id']];
            if($_d[2])$data[$k]['unit']['unit3']['id'] = $Unit['extra2_unit_id']                       ;
            if($_d[2])$data[$k]['unit']['unit3']['name'] = $Units[$Unit['extra2_unit_id']];
            if($ClientGoodsPriceData!=0){
                $data[$k]['unit']['unit1']['price'] =$ClientGoodsPriceData['client_price1'];
                if($_d[1]) $data[$k]['unit']['unit2']['price']=$ClientGoodsPriceData['client_price2'];
                if($_d[2]) $data[$k]['unit']['unit3']['price']=$ClientGoodsPriceData['client_price3'];

            }else{
                $GoodsLevelPrice = $GoodsLevelPriceDb->where('goods_id',$v['goods_id'])->where('level_id',$level_id)->field('level_price1,level_price2,level_price3')->find();
                if($GoodsLevelPrice){
                    $data[$k]['unit']['unit1']['price'] =$GoodsLevelPrice['level_price1'];
                    if($_d[1]) $data[$k]['unit']['unit2']['price']=$GoodsLevelPrice['level_price2'];
                    if($_d[2]) $data[$k]['unit']['unit3']['price']=$GoodsLevelPrice['level_price3'];

                }else{
                    $data[$k]['unit']['unit1']['price'] =$Unit['initial_unit_price'];
                    if($_d[1]) $data[$k]['unit']['unit2']['price']=$Unit['extra1_unit_price'];
                    if($_d[2]) $data[$k]['unit']['unit3']['price']=$Unit['extra2_unit_price'];
                }
            }
            $data[$k]['specification'] = $data[$k]['specification1_name'].':'.$data[$k]['specification2_value'].','.$data[$k]['specification1_name'].':'.$data[$k]['specification2_value'].','.$data[$k]['specification3_name'].':'.$data[$k]['specification3_value'];





        }
        return $data;
    }
    /**
     * 搜索(区别用户 列表)
     */
    public function search3($params){
        $page = !empty($params['page']) ? $params['page'] : 1;
        $pageSize = !empty($params['pageSize']) ? $params['pageSize'] : 20;
        $where = [];


        //是否有登陆



        $data = $this->GoodsDb()->alias('goods')
            ->where($where)
            ->field('goods.goods_id,goods.goods_sn,goods.goods_name,goods.specification1_name,goods.specification2_name,goods.specification3_name,goods.specification1_value,goods.specification2_value,goods.specification3_value')
            ->where(  'goods.is_delete' ,0)
            ->where('goods_state',1)
            ->page($page,$pageSize)
            ->order('goods.add_goods_time desc')
            ->select();

        return $data;
    }
    /**
     * 单获取商品价格
     * $goods_id 商品id
     * $client_id 客户id
     * $unit_level 单位等级（0最小单位，1副单位1,2副单位2）
     * $num   起订量
     */
    public function goodsPrice($goods_id,$client_id,$unit_level,$num){

        $where = [];
        //单个商品
        $GoodsUnits = new GoodsUnits();
        $GoodsUnitsDb = $GoodsUnits->GoodsUnitsDb();
        //判断是否有客户独立价格
        $GoodsClientPrice = new GoodsClientPrice();
        $GoodsClientPriceDb = $GoodsClientPrice->GoodsClientPriceDb();
        $where['client_id'] = $client_id;
        $where['goods_id'] =  $goods_id;
        $ClientGoodsPriceData = $this->getClientGoodsPrice($client_id,$goods_id);
        $GoodsUnitData = $GoodsUnitsDb->where('goods_id',$goods_id)->find();
        //客户字段转换
        //单位字段转换
        //等级字段装换
        $s = '';
        $s2 = '';
        $s3 = '';
        switch ($unit_level){
            case 0:
                $s = 'client_price1';
                $s2 = 'initial_unit_price';
                $s3 = 'level_price1';
                break;
            case 1:
                $s = 'client_price2';
                $s2 = 'extra1_unit_price';
                $s3= 'level_price2';
                break;
            case 2:
                $s = 'client_price3';
                $s2 = 'extra2_unit_price';
                $s3 = 'level_price3';
                break;
        }
        //默认价格
        $GoodsUnits = new GoodsUnits();
        $GoodsUnitsDb = $GoodsUnits->GoodsUnitsDb();
        $dP = $GoodsUnitsDb->where('goods_id',$goods_id)->value($s2);
        $retP = 0;
        if($ClientGoodsPriceData!=0){//you客户

            //判断是否可购
            if($ClientGoodsPriceData['client_purchase']==0){//允许

                //判断是否达到起订要求
                if($ClientGoodsPriceData['client_num']>=$num){//满足
                    $retP = $ClientGoodsPriceData[$s];
                }else{//不满足
                    $retP = $dP;
                }
            }else{//不允许
                $retP = $dP;
            }

        }else{//wu客户

            $Client = new Client();
            $client_id = $Client->where('client_id',$client_id)->value('level_id');
            //查询等级是否you
            $GoodsLevelPrice = new GoodsLevelPrice();
            $GoodsLevelPriceDb = $GoodsLevelPrice->GoodsLevelPriceDb();
            $GoodsLevelPriceData = $GoodsLevelPriceDb->where('level_id',$client_id)->where('goods_id',$goods_id)->find();
            if($GoodsLevelPriceData){//you
                //判断是否可购
                if($GoodsLevelPriceData['level_purchase']==0){//允许
                    //判断起订量

                    if($ClientGoodsPriceData['client_num']>=$num){//满足
                        $retP = $GoodsLevelPriceData[$s3];
                    }else{//不满足
                        $retP = $dP;

                    }
                }else{//不允许
                    $retP = $dP;
                }

            }
        }
        return $retP;
    }

    //查询是否有指定客户可购
    public function getClientGoodsPrice($client_id,$goods_id){
        $GoodsClientPrice = new GoodsClientPrice();
        $GoodsClientPriceDb = $GoodsClientPrice->GoodsClientPriceDb();
        $where['client_id'] = $client_id;
        $where['goods_id'] = $goods_id;
        $data = $GoodsClientPriceDb->where($where)->find();
        if (empty($data)){
            return 0;
        }else{
            return $data;
        }

    }
    /**
     * 商品详情
     */

    /**
     * 商品详情后期处理
     */
    public function infoAfter()
    {
        $count = count($this->skus);
        for ($i = 0; $i < $count; $i++) {
            $currentSku = $this->skus[$i];

            // 商品信息不全 使用默认规格的信息
            if ($this->sku_default === $currentSku->sku_id) {
                $this->defaultSku = $currentSku;
                empty($this->goods_images) && $this->goods_images = $currentSku->sku_images;
                empty($this->price) && $this->price = $currentSku->price;
            }

            // 当前规格没有信息 使用商品信息
            empty($currentSku->sku_name) && $currentSku->sku_name = $this->goods_name;
            empty($currentSku->sku_images) && $currentSku->sku_images = $this->goods_images;
            $currentSku->defaultImage = $currentSku->sku_images[0];
            empty($currentSku->price) && $currentSku->price = $this->price;
        }

        // 第一张图作为默认图片
        $this->defaultImage = $this->goods_images[0];

        $this->doWithSpecs();

        return $this;
    }

    public function doWithSpecs()
    {
        $specsValues = [];
        $specsValuesIds = [];
        $count = count($this->specs);
        for ($i = 0; $i < $count; $i++) {
            $currentSpec = $this->specs[$i];
            // array_column($currentSpec->values, 'value_id')
            $currentSpec->valuesIds = array_map(function($value) {
                return $value->value_id;
            }, $currentSpec->values);
            $specsValues[] = $currentSpec->valuesIds;
            $specsValuesIds = array_merge($specsValuesIds, $currentSpec->valuesIds);
        }
        $this->specsValues = $specsValues;
        $this->specsValuesIds = $specsValuesIds;

        $count = count($this->skus);
        for ($i = 0; $i < $count; $i++) {
            $currentSku = $this->skus[$i];
            $currentSku->specsArray = array_values($currentSku->specs);
        }

        return $this;
    }

    public function gIdAndUnitPrice($goods_id,$unit_id,$client_id){
        $GoodsDb = $this->GoodsDb();
        $where['g.goods_id'] = $goods_id;
        $data = $GoodsDb->alias('g')->
        join('sh_goods_units u','g.goods_id = u.goods_id')->
        where($where)->field('g.goods_id,g.keywords,g.goods_img,g.goods_sn,g.goods_name,g.specification1_name,g.specification2_name,g.specification3_name,g.specification1_value,g.specification2_value,g.specification3_value')->find();
        $data['unit_id'] = $unit_id;
        $GoodsUnitsModel = new Unit();
        $data['unit_name'] = $GoodsUnitsModel->where('unit_id',$unit_id)->value('unit_name');
        $ClientModel = new Client();
        $clientDb = $ClientModel->ClientDb();
        $GoodsUnitModel = new GoodsUnits();
        $ClientGoodsPriceData = $this->getClientGoodsPrice($client_id,$data['goods_id']);
        $GoodsUnitsDb = $GoodsUnitModel->GoodsUnitsDb();
        $Unit = $GoodsUnitsDb->where('goods_id',$data['goods_id'])->find();
        $level_id = $clientDb->where('client_id',$client_id)->value('level_id');
        $GoodsLevelPriceModel =  new GoodsLevelPrice();
        $GoodsLevelPriceDb = $GoodsLevelPriceModel->GoodsLevelPriceDb();
        $unit = [];
        //判断是否有客户价格
        if($ClientGoodsPriceData!=0){
            $unit[$Unit['initial_unit_id']] =$ClientGoodsPriceData['client_price1'];
            $unit[$Unit['extra1_unit_id']] =$ClientGoodsPriceData['client_price2'];
            $unit[$Unit['extra2_unit_id']] =$ClientGoodsPriceData['client_price3'];
        }else{
            $GoodsLevelPrice = $GoodsLevelPriceDb->where('goods_id',$data['goods_id'])->where('level_id',$level_id)->field('level_price1,level_price2,level_price3')->find();
            //判断是否有等级价格
            if($GoodsLevelPrice){
                $unit[$Unit['initial_unit_id']] = $GoodsLevelPrice['level_price1'];
                $unit[$Unit['extra1_unit_id']] = $GoodsLevelPrice['level_price2'];
                $unit[$Unit['extra2_unit_id']] = $GoodsLevelPrice['level_price3'];
            }else{//如果都没有就使用单位默认价格
                $unit[$Unit['initial_unit_id']] =$Unit['initial_unit_price'];
                $unit[$Unit['extra1_unit_id']]=$Unit['extra1_unit_price'];
                $unit[$Unit['extra2_unit_id']]=$Unit['extra2_unit_price'];
            }
        }

        $data['price'] = $unit[$unit_id];
        return $data;
    }

    public function allGoodsList($params){
        $page = !empty($params['page']) ? $params['page'] : 1;
        $pageSize = !empty($params['pageSize']) ? $params['pageSize'] : 10;
        $where = [];
        //搜索
        if(!empty($params['query'])){
            $where['goods.goods_name'] = ['like','%'.$params['query'].'%'];
        }
        //商品分类id
        if(!empty($params['cate_id'])){
            $where['goods.cate_id'] = $params['cate_id'];
        }

        //获取客户等级
        $ClientModel = new Client();
        $clientDb = $ClientModel->ClientDb();
        $clientData = $clientDb->where('client_id',$params['client_id'])->find();
        if(empty($clientData)){
            $pager = [
                'page' => $page,
                'pageSize' => $pageSize,
                'total' => 0
            ];
    
            return [
                'data' => [],
                'pager' => $pager
            ];
        }
        $where['level_id'] = $clientData['level_id'];

        $data = $this->GoodsDb()->alias('goods')
            ->join('sh_goods_level_price level','level.goods_id = goods.goods_id','left')
            ->join('sh_category category','goods.cate_id = category.cate_id')
            ->where($where)
            ->where(  'goods.is_delete' ,0)
            ->where('goods_state',1)
            ->field('goods.goods_id,goods.is_units,category.cate_name,goods.specification_id,goods.goods_img,goods.goods_sn,goods.goods_name,goods.specification1_name,goods.specification2_name,goods.specification3_name,goods.specification1_value,goods.specification2_value,goods.specification3_value')
            ->page($page,$pageSize)
            ->order('goods.add_goods_time desc')
            ->select();
        $total = $this->GoodsDb()->alias('goods')
            ->join('sh_goods_level_price level','level.goods_id = goods.goods_id','left')
            ->where($where)
            ->where(  'goods.is_delete' ,0)
            ->where('goods_state',1)
            ->field('goods.goods_id')
            ->count();

        $GoodsUnitModel = new GoodsUnits();
        $GoodsUnitsDb = $GoodsUnitModel->GoodsUnitsDb();
        $GoodsUnitsModel = new Unit();
        $Units =  $GoodsUnitsModel->where('is_delete',0)->column('unit_name','unit_id');

        $level_id = $ClientModel->where('client_id',$params['client_id'])->value('level_id');
        $GoodsLevelPriceModel =  new GoodsLevelPrice();
        $GoodsLevelPriceDb = $GoodsLevelPriceModel->GoodsLevelPriceDb();
        $retData = [];

        //优惠规则
        $discount = $this->getpromotion($params['client_id']);

        foreach ($data as $k=>$v){

            foreach($discount['product'] as $p){
                if($v['goods_id'] == $p['promotion_goods']){
                    $data[$k]['promotion'] = $p;
                }
            }

            foreach($discount['group'] as $g){
                $tmp = explode(',',$g['group_goods']);
                if(in_array($v['goods_id'],$tmp)){
                    $data[$k]['promotion'] = $g;
                }
            } 

            $data[$k]['dis_tag'] = '';

            if(!empty($data[$k]['promotion'])){
                switch($data[$k]['promotion']['promotion_rule']){
                    case 2:
                        $data[$k]['dis_tag'] = "【满减】";break;
                    case 3:
                        $data[$k]['dis_tag'] = "【满折】";break;
                    case 5:
                        $data[$k]['dis_tag'] = "【直降】";break;
                    case 6:
                        $data[$k]['dis_tag'] = "【打折】";break;
                }
            }

            $ClientGoodsPriceData = $this->getClientGoodsPrice($params['client_id'],$v['goods_id']);
            $Unit = $GoodsUnitsDb->where('goods_id',$v['goods_id'])->find();
            //判断单位是否可购
            $_d[0] = $Unit['is_use_initial_unit'];
            $_d[1]= $v['is_units'] && $Unit['is_use_extra1_unit'] ? $Unit['is_use_extra1_unit'] : 0;
            $_d[2] = $v['is_units'] && $Unit['is_use_extra2_unit'] ? $Unit['is_use_extra2_unit'] : 0;

            $data[$k]['unit_id'] =$Unit['initial_unit_id'];
            $data[$k]['unit_name'] = $Units[$Unit['initial_unit_id']];
            $data[$k]['quantity'] = 1;

            $str = '';
            if(!empty($v['specification1_name'])){
                $str .= $v['specification1_name'].' : '.$v['specification1_value'];
            }
            if(!empty($v['specification2_name'])){
                $str .= ','.$v['specification2_name'].' : '.$v['specification2_value'];
            }
            if(!empty($v['specification3_name'])){
                $str .= ','.$v['specification3_name'].' : '.$v['specification3_value'];
            }

            $str = !empty($str) ? '【'.$str.'】' : $str;

            $data[$k]['specification'] = $str;
            $data[$k]['show_name'] = $data[$k]['dis_tag'].' '.$data[$k]['goods_name'].' '.$str;

            if($ClientGoodsPriceData!=0){
                $data[$k]['price'] = $ClientGoodsPriceData['client_price1'];
                $price1 = $ClientGoodsPriceData['client_price1'];
                if($_d[1])$price2=$ClientGoodsPriceData['client_price2'];
                if($_d[2])$price3=$ClientGoodsPriceData['client_price3'];
            }else{
                $GoodsLevelPrice = $GoodsLevelPriceDb->where('goods_id',$v['goods_id'])->where('level_id',$level_id)->field('level_price1,level_price2,level_price3,level_num')->find();
                if($GoodsLevelPrice){
                    $data[$k]['price'] = $GoodsLevelPrice['level_price1'];
                    $price1 = $GoodsLevelPrice['level_price1'];
                    if($_d[1])$price2=$GoodsLevelPrice['level_price2'];
                    if($_d[2])$price3=$GoodsLevelPrice['level_price3'];
                }else{
                    $data[$k]['price'] = $Unit['initial_unit_price'];
                    $price1 = $Unit['initial_unit_price'];
                    if($_d[1])$price2=$Unit['extra2_unit_price'];
                    if($_d[2])$price3=$Unit['extra3_unit_price'];
                }
            }

            $data[$k]['unit'][] = [
                'id' => $Unit['initial_unit_id'],
                'name' => $Units[$Unit['initial_unit_id']],
                'price' => $price1
            ];

            if($_d[1]){
                $data[$k]['unit'][] = [
                    'id' => $Unit['extra1_unit_id'],
                    'name' => $Units[$Unit['extra1_unit_id']],
                    'price' => $price2,
                    'ratio' => $Unit['extra1_unit_ratio'],
                ];
            }

            if($_d[2]){
                $data[$k]['unit'][] = [
                    'id' => $Unit['extra2_unit_id'],
                    'name' => $Units[$Unit['extra2_unit_id']],
                    'price' => $price3,
                    'ratio' => $Unit['extra2_unit_ratio'],
                ];
            }
        }
        $pager = [
            'page' => $page,
            'pageSize' => $pageSize,
            'total' => $total
        ];

        return [
            'data' => $data,
            'pager' => $pager
        ];
    }

    public function getpromotion($client_id){
        $Pro=new PromotionModel();
        $level_id=db("client")->where(["client_id"=>$client_id])->value("level_id");
        $now=date("Y-m-d H:i:s");
        $wherec="cus_list=$client_id or cus_list like '{$client_id},%' or cus_list like '%,{$client_id}' or cus_list like '%,{$client_id},%'";
        $whered="start_time<='{$now}' and end_time>'$now' ";

        $pc=db("promotion")->where($wherec)->where($whered)->where("promotion_cate=1")->field("promotion_id,promotion_cate,promotion_goods,promotion_title,promotion_rule,promotion_detail,start_time,end_time")->select();
        $pc1=db("promotion")->where(["cus_level"=>$level_id])->where($whered)->where("promotion_cate=1")->field("promotion_id,promotion_cate,promotion_goods,promotion_title,promotion_rule,promotion_detail,start_time,end_time")->select();
        $pc2=db("promotion")->where(["cus_level"=>9999])->where($whered)->where("promotion_cate=1")->field("promotion_id,promotion_cate,promotion_goods,promotion_title,promotion_rule,promotion_detail,start_time,end_time")->select();
        $pids=[];
        $product=[];
        foreach ($pc as $k => $v) {
            # code...
            $pc[$k]["promotion_detail"]=json_decode($pc[$k]["promotion_detail"],true);
            $pids[]=$v["promotion_goods"];
            $product=$pc[$k];
        }
        foreach ($pc1 as $k => $v) {
            # code...
            if(!in_array($v["promotion_goods"],$pids)){
                $pc1[$k]["promotion_detail"]=json_decode($pc1[$k]["promotion_detail"],true);
                $pids[]=$v["promotion_goods"];
                $product[]=$pc1[$k];
            }
            
        }
        foreach ($pc2 as $k => $v) {
            # code...
            if(!in_array($v["promotion_goods"],$pids)){
                $pc2[$k]["promotion_detail"]=json_decode($pc2[$k]["promotion_detail"],true);
                $pids[]=$v["promotion_goods"];
                $product[]=$pc2[$k];
            }
            
        }
        $data["product"]=$product;
        $gc=db("promotion")->where($wherec)->where($whered)->where("promotion_cate=3")->field("promotion_id,promotion_cate,group_goods,group_goods_count,promotion_type,promotion_title,promotion_rule,promotion_detail,start_time,end_time")->select();
        $gc1=db("promotion")->where(["cus_level"=>$level_id])->where($whered)->where("promotion_cate=3")->field("promotion_id,promotion_cate,group_goods,group_goods_count,promotion_type,promotion_title,promotion_rule,promotion_detail,start_time,end_time")->select();
        $gc2=db("promotion")->where(["cus_level"=>9999])->where($whered)->where("promotion_cate=3")->field("promotion_id,promotion_cate,group_goods,group_goods_count,promotion_type,promotion_title,promotion_rule,promotion_detail,start_time,end_time")->select();
        $pidgs=[];
        $group=[];
        foreach ($gc as $k => $v) {
            # code...
            $gc[$k]["promotion_detail"]=json_decode($gc[$k]["promotion_detail"],true);
            $p=explode(',',$v["group_goods"]);
            for($i=0;$i<count($p);$i++){
                $pidgs[]=$p[$i];
            }
            $group[]=$gc[$k];
        }
        foreach ($gc1 as $k => $v) {
            # code...
            $p=explode(',',$v["group_goods"]);
            $flag=0;
            for($i=0;$i<count($p);$i++){
                if(in_array($p[$i],$pidgs)){
                    $flag=1;break;
                }
            }
            if($flag==0){
                $gc1[$k]["promotion_detail"]=json_decode($gc1[$k]["promotion_detail"],true);

                for($i=0;$i<count($p);$i++){
                    $pidgs[]=$p[$i];
                }
                $group[]=$gc1[$k];
            }
            
        }
        foreach ($gc2 as $k => $v) {
            # code...
            $p=explode(',',$v["group_goods"]);
            $flag=0;
            for($i=0;$i<count($p);$i++){
                if(in_array($p[$i],$pidgs)){
                    $flag=1;break;
                }
            }
            if($flag==0){
                $gc2[$k]["promotion_detail"]=json_decode($gc2[$k]["promotion_detail"],true);

                for($i=0;$i<count($p);$i++){
                    $pidgs[]=$p[$i];
                }
                $group[]=$gc2[$k];
            }
            
        }
        $data["group"]=$group;

        return $data;
    }

    public function goodsList($params){

        $specification_id= $params['specification_id'];
        $goods_id=$params['goods_id'];
        $request = request();
        $client_id = $params['client_id'];
        $goodsModel = new \app\shop\model\Goods();
        $GoodsDb = $goodsModel->GoodsDb();
        $GoodsLevelPrice = new GoodsLevelPrice();
        $GoodsLevelPriceDb = $GoodsLevelPrice->GoodsLevelPriceDb();
        $GoodsClientPrice = new GoodsClientPrice();
        $GoodsClientPriceDb = $GoodsClientPrice->GoodsClientPriceDb();
        $GoodsImg = new GoodsImg();
        $GoodsimgDb = $GoodsImg->GoodsimgDb();
        $GoodsAccessory = new  GoodsAccessory();
        $GoodsAccessoryDb = $GoodsAccessory->GoodsAccessoryDb();
        $ClientModel = new Client();
        $clientDb = $ClientModel->ClientDb();
        $clientData = $clientDb->where('client_id',$client_id)->find();
        $level_id = $clientData['level_id'];
        $data = $this->GoodsDb()->alias('goods')
            ->join('sh_goods_level_price level','level.goods_id = goods.goods_id','left')
            ->join('sh_category category','goods.cate_id = category.cate_id')
            ->field('goods.goods_id,goods.goods_name,goods.is_units,category.cate_name,goods.specification_id,goods.goods_img,goods.goods_sn,goods.goods_name,goods.specification1_name,goods.specification2_name,goods.specification3_name,goods.specification1_value,goods.specification2_value,goods.specification3_value')
            ->where('level.level_id',$level_id)
            ->where('goods.specification_id',$specification_id)
            ->select();
        $rGoodsData = [];
        $GoodsUnits = new GoodsUnits();
        $GoodsUnitsDb = $GoodsUnits->GoodsUnitsDb();
        $GoodsUnitsModel = new Unit();
        $Units =  $GoodsUnitsModel->where('is_delete',0)->column('unit_name','unit_id');

        $finalGoods = [];

        //优惠规则
        $discount = $this->getpromotion($params['client_id']);
        
        foreach ($data as $k=>&$v){

            foreach($discount['product'] as $p){
                if($v['goods_id'] == $p['promotion_goods']){
                    $v['promotion'] = $p;
                }
            }

            foreach($discount['group'] as $g){
                $tmp = explode(',',$g['group_goods']);
                if(in_array($v['goods_id'],$tmp)){
                    $v['promotion'] = $g;
                }
            } 

            $v['dis_tag'] = '';
            
            if(!empty($v['promotion'])){
                switch($v['promotion']['promotion_rule']){
                    case 2:
                        $v['dis_tag'] = "【满减】";break;
                    case 3:
                        $v['dis_tag'] = "【满折】";break;
                    case 5:
                        $v['dis_tag'] = "【直降】";break;
                    case 6:
                        $v['dis_tag'] = "【打折】";break;
                }
            }

            $str = '';
            if(!empty($v['specification1_name'])){
                $str .= $v['specification1_name'].' : '.$v['specification1_value'];
            }
            if(!empty($v['specification2_name'])){
                $str .= ','.$v['specification2_name'].' : '.$v['specification2_value'];
            }
            if(!empty($v['specification3_name'])){
                $str .= ','.$v['specification3_name'].' : '.$v['specification3_value'];
            }

            $str = !empty($str) ? '【'.$str.'】' : $str;

            $v['specification'] = $str;
            
            $v['show_name'] = $v['dis_tag'].' '.$v['goods_name'].' '.$str;

            //判断单位是否可购
            $Unit = $GoodsUnitsDb->where('goods_id',$v['goods_id'])->find();
            $_d[0] = $Unit['is_use_initial_unit'];
            $_d[1]= $v['is_units'] && $Unit['is_use_extra1_unit'] ? $Unit['is_use_extra1_unit'] : 0;
            $_d[2] = $v['is_units'] && $Unit['is_use_extra2_unit'] ? $Unit['is_use_extra2_unit'] : 0;
            $rGoodsData[$k] = $v;
            $clientPrice = $GoodsClientPriceDb->where('goods_id',$v['goods_id'])->where('client_id',$client_id)->where('client_purchase',0)->find();
            
            $v['unit_id'] =$Unit['initial_unit_id'];
            $v['unit_name'] = $Units[$Unit['initial_unit_id']];
            $v['price'] = $Unit['initial_unit_price'];
            $v['quantity'] = 1;

            //获取价格
            if(!empty($clientPrice)){
                $price1 = $ClientGoodsPriceData['client_price1'];
                if($_d[1])$price2=$ClientGoodsPriceData['client_price2'];
                if($_d[2])$price3=$ClientGoodsPriceData['client_price3'];
            }else{
                $clientLevel = $GoodsLevelPriceDb->where('goods_id',$v['goods_id'])->where('level_id',$level_id)->field('level_price1,level_price2,level_price3')->find();
                if($GoodsLevelPrice){
                    $price1 = $clientLevel['level_price1'];
                    if($_d[1])$price2=$clientLevel['level_price2'];
                    if($_d[2])$price3=$clientLevel['level_price3'];
                }else{
                    $price1 = $Unit['initial_unit_price'];
                    if($_d[1])$price2=$Unit['extra2_unit_price'];
                    if($_d[2])$price3=$Unit['extra3_unit_price'];
                }
            }

            $v['unit'][] = [
                'id' => $Unit['initial_unit_id'],
                'name' => $Units[$Unit['initial_unit_id']],
                'price' => $price1,
            ];

            if($_d[1]){
                $v['unit'][] = [
                    'id' => $Unit['extra1_unit_id'],
                    'name' => $Units[$Unit['extra1_unit_id']].'('.$Unit['extra1_unit_ratio'].$Units[$Unit['initial_unit_id']].')',
                    'price' => $price2,
                    'ratio' => $Unit['extra1_unit_ratio'],
                ];
            }

            if($_d[2]){
                $v['unit'][] = [
                    'id' => $Unit['extra2_unit_id'],
                    'name' => $Units[$Unit['extra2_unit_id']].'('.$Unit['extra2_unit_ratio'].$Units[$Unit['initial_unit_id']].')',
                    'price' => $price3,
                    'ratio' => $Unit['extra1_unit_ratio'],
                ];
            }

            $tmp1 = [
                'unit_id' => $Unit['initial_unit_id'],
                'unit_name' => $Units[$Unit['initial_unit_id']],
                'price' => $price1,
                'origin_price' => $Unit['initial_unit_price']
            ];
            $finalGoods[] = array_merge($v,$tmp1);

            if($_d[1]){
                $tmp2 = [
                    'unit_id' => $Unit['extra1_unit_id'],
                    'unit_name' => $Units[$Unit['extra1_unit_id']].'('.$Unit['extra1_unit_ratio'].$Units[$Unit['initial_unit_id']].')',
                    'price' => $price2,
                    'origin_price' => $Unit['extra1_unit_price']
                ];
                $finalGoods[] = array_merge($v,$tmp2);
            }

            if($_d[2]){
                $tmp3 = [
                    'unit_id' => $Unit['extra2_unit_id'],
                    'unit_name' => $Units[$Unit['extra2_unit_id']].'('.$Unit['extra2_unit_ratio'].$Units[$Unit['initial_unit_id']].')',
                    'price' => $price3,
                    'origin_price' => $Unit['extra2_unit_price']
                ];
                $finalGoods[] = array_merge($v,$tmp3);
            }
        }
        return $finalGoods;
    }

    public function adminGoods($params){
        $page = !empty($params['page']) ? $params['page'] : 1;
        $pageSize = !empty($params['pageSize']) ? $params['pageSize'] : 10;
        $where = [];
        if(!empty($params['cate_id'])){
            $where['cate_id'] = $params['cate_id'];
        }
        if(isset($params['goods_state'])){
            $where['goods_state'] = $params['goods_state'];
        }
        if(!empty($params['query'])){
            $where['goods_name | goods_sn | keywords'] = ['like','%'.$params['query'].'%'];
        }
        if(isset($params['is_new'])){
            $where['is_new'] = 1;
        }
        if(isset($params['is_hot'])){
            $where['is_hot'] = 1;
        }
        if(!empty($params['warehouse_id'])){
            $where['warehouse_id'] = ['in',$params['warehouse_id']];
        }
        if(isset($params['no_img'])){
            $where['goods_img'] = '';
        }
        if(isset($params['goods_activity_state'])){
            $where['goods_activity_state'] = $params['goods_activity_state'];
        }
        $goodsModel = new \app\shop\model\Goods();
        $GoodsDb = $goodsModel->GoodsDb();
        $data = $GoodsDb->alias('a')
                ->join('sh_goods_units b','a.goods_id = b.goods_id')
                ->join('sh_goods_unit c','b.initial_unit_id = c.unit_id')
                ->field('a.goods_id,a.goods_img,a.specification_id,a.goods_name,a.goods_sn,a.specification1_name,a.specification2_name,a.specification3_name,a.specification1_value,a.specification2_value,a.specification3_value,a.cost_price,a.goods_inventory,add_goods_time,update_goods_time,b.initial_unit_price,c.unit_name')
                ->where($where)
                ->page($page,$pageSize)
                ->select();
        $total = $GoodsDb->alias('a')
                ->join('sh_goods_units b','a.goods_id = b.goods_id')
                ->join('sh_goods_unit c','b.initial_unit_id = c.unit_id')
                ->field('a.goods_id')
                ->where($where)
                ->count();
        $GoodsLevelPrice = new GoodsLevelPrice();
        $GoodsLevelPriceDb = $GoodsLevelPrice->GoodsLevelPriceDb();
        foreach($data as &$v){
            $str = '';
            if(!empty($v['specification1_name'])){
                $str .= $v['specification1_name'].' : '.$v['specification1_value'];
            }
            if(!empty($v['specification2_name'])){
                $str .= ','.$v['specification2_name'].' : '.$v['specification2_value'];
            }
            if(!empty($v['specification3_name'])){
                $str .= ','.$v['specification3_name'].' : '.$v['specification3_value'];
            }
            $v['specification'] = $str;
            $v['add_goods_time'] = !empty($v['add_goods_time']) ? date('Y-m-d H:i:s',$v['add_goods_time']) : '';
            $v['update_goods_time'] = !empty($v['update_goods_time']) ? date('Y-m-d H:i:s',$v['update_goods_time']) : '';
            $v['level_price'] = $GoodsLevelPriceDb->alias('a')
                                ->join('sh_client_level b','a.level_id = b.level_id')
                                ->where('a.goods_id',$v['goods_id'])
                                ->field('a.level_price1 price,b.level_name name')
                                ->select();
        }
        $final = [
            'data' => $data,
            'pager' => [
                'page' => $page,
                'pageSize' => $pageSize,
                'total' => $total
            ]
        ];
        return $final;
    }

    /**
     * 商品详情
     */
    public function info($params){

        $specification_id= $params['specification_id'];
        $goods_id=$params['goods_id'];
        $GoodsDesc = new \app\shop\model\Goodsdescribe();
        $GoodsDescDb = $GoodsDesc->GoodsdescribeDb();
        $desc = $GoodsDescDb->where('goods_id',$goods_id)->value('describe_content');
        $request = request();
        $goodsModel = new \app\shop\model\Goods();
        $GoodsDb = $goodsModel->GoodsDb();
        $GoodsImg = new GoodsImg();
        $GoodsimgDb = $GoodsImg->GoodsimgDb();
        $GoodsAccessory = new  GoodsAccessory();
        $GoodsAccessoryDb = $GoodsAccessory->GoodsAccessoryDb();
        $specificationName = [];
        $specificationValue = [[],[],[]];
        $specificationThis = ['','',''];
        $specificationGoods = $GoodsDb->where('specification_id',$specification_id)->select();

        if(count($specificationGoods)>0){

            $specificationName=[$specificationGoods[0]['specification1_name'],$specificationGoods[0]['specification2_name'],$specificationGoods[0]['specification3_name']];
        }
        $rGoodsData = [];
        $goods_ids = [];
        $GoodsUnits = new GoodsUnits();
        $GoodsUnitsDb = $GoodsUnits->GoodsUnitsDb();
        $GoodsUnitsModel = new Unit();
        $Units =  $GoodsUnitsModel->where('is_delete',0)->column('unit_name','unit_id');
        foreach ($specificationGoods as $k=>$v){
            $goods_ids[] = $v['goods_id'];
            //判断单位是否可购
            $Unit = $GoodsUnitsDb->where('goods_id',$v['goods_id'])->find();
            $_d[0] = $Unit['is_use_initial_unit'];
            $_d[1]= $v['is_units'] && $Unit['is_use_extra1_unit'] ? $Unit['is_use_extra1_unit'] : 0;
            $_d[2] = $v['is_units'] && $Unit['is_use_extra2_unit'] ? $Unit['is_use_extra2_unit'] : 0;
            if($v['goods_id']==$goods_id){
                $specificationThis[0] =  $v['specification1_value'];
                $specificationThis[1] =  $v['specification2_value'];
                $specificationThis[2] =  $v['specification3_value'];
            }
            if(!in_array($v['specification1_value'],$specificationValue[0]))$specificationValue[0][] = $v['specification1_value'];
            if(!in_array($v['specification2_value'],$specificationValue[1]))$specificationValue[1][] = $v['specification2_value'];
            if(!in_array($v['specification3_value'],$specificationValue[2]))$specificationValue[2][] = $v['specification3_value'];
            $rGoodsData[$k] = $v;
            $rGoodsData[$k]['cate_name'] = Db::name('category')->where('cate_id',$v['cate_id'])->value('cate_name');
            $rGoodsData[$k]['produced_time'] = !empty($v['produced_time'])?'':date('Y-m-d',strtotime($v['produced_time']));
            
            //获取价格
            $rGoodsData[$k]['price'] = $Unit['initial_unit_price'];
            $price1 = $Unit['initial_unit_price'];
            if($_d[1])$price2=$Unit['extra1_unit_price'];
            if($_d[2])$price3=$Unit['extra2_unit_price'];

            $rGoodsData[$k]['unit'][] = [
                'id' => $Unit['initial_unit_id'],
                'name' => $Units[$Unit['initial_unit_id']],
                'price' => $price1,
                'origin_price' => $Unit['initial_unit_price']
            ];

            if($_d[1]){
                $rGoodsData[$k]['unit'][] = [
                    'id' => $Unit['extra1_unit_id'],
                    'name' => $Units[$Unit['extra1_unit_id']].'('.$Unit['extra1_unit_ratio'].$Units[$Unit['initial_unit_id']].')',
                    'price' => $price2,
                    'origin_price' => $Unit['extra1_unit_price']
                ];
            }

            if($_d[2]){
                $rGoodsData[$k]['unit'][] = [
                    'id' => $Unit['extra2_unit_id'],
                    'name' => $Units[$Unit['extra2_unit_id']].'('.$Unit['extra2_unit_ratio'].$Units[$Unit['initial_unit_id']].')',
                    'price' => $price3,
                    'origin_price' => $Unit['extra2_unit_price']
                ];
            }

            $rGoodsData[$k]['goods_accessory'] = $GoodsAccessoryDb->alias('b')->join('sh_accessory c','b.accessory_id=c.accessory_id')->where('b.goods_id',$v['goods_id'])->select();
        }

        //所有图片
        $imgData = $GoodsimgDb->alias('a')->join('sh_img i','i.img_id=a.img_id')->where('a.goods_id','in',$goods_ids)->column('img_url');

        $redData = [
            'specificationThis'=>$specificationThis,
            'specificationName'=>$specificationName,
            'specificationValue'=>$specificationValue,
            'goodsData'=>$rGoodsData,
            'imgData' => $imgData,
            'desc' => $desc
        ];
        return $redData;
    }

    //选择关联产品
    public function relation_goods($params){

        $where = [];

        if(!empty($params['query'])){
            $where['goods_name | goods_sn'] = ['like','%'.$params['query'].'%'];
        }

        if(!empty($params['cate_id'])){
            $where['a.cate_id'] = ['in',$params['cate_id']];
        }

        $data = Db::name('goods')->alias('a')
        ->join('sh_category b','a.cate_id = b.cate_id')
        ->where($where)
        ->field('a.goods_id,a.goods_img,a.goods_name,b.cate_name,specification1_name,specification2_name,specification3_name,specification1_value,specification2_value,specification3_value')
        ->select();

        foreach($data as &$v){
            $v['is_checked'] = false;
            $str = '';
            if(!empty($v['specification1_name'])){
                $str = $v['specification1_name'].':'.$v['specification1_value'];
            }

            if(!empty($v['specification2_name'])){
                $str .= ','.$v['specification2_name'].':'.$v['specification2_value'];
            }

            if(!empty($v['specification3_name'])){
                $str .= ','.$v['specification3_name'].':'.$v['specification3_value'];
            }

            if(!empty($str)){
                $str = "【 ".$str." 】";
            }

            $v['specification'] = $str;
        }

        return $data;

    }


    public function show_goods($params){

        $page = !empty($params['page']) ? $params['page'] : 1;
        $pageSize = !empty($params['pageSize']) ? $params['pageSize'] : 10;

        $goods = Db::name('goods')->page($page,$pageSize)->select();

        $total = Db::name('goods')->field('goods_id')->count();

        foreach($goods as &$v){
            $str = '';
            if(!empty($v['specification1_name'])){
                $str = $v['specification1_name'].':'.$v['specification1_value'];
            }

            if(!empty($v['specification2_name'])){
                $str .= ','.$v['specification2_name'].':'.$v['specification2_value'];
            }

            if(!empty($v['specification3_name'])){
                $str .= ','.$v['specification3_name'].':'.$v['specification3_value'];
            }

            if(!empty($str)){
                $str = "【 ".$str." 】";
            }

            $v['specification'] = $str;
        }

        return [
            'data' => $goods,
            'pager' => [
                'page' => $page,
                'pageSize' => $pageSize,
                'total' => $total
            ]
        ];

    }

}